package com.wsl.tibet_sprcifcation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wsl.pojo.Tb_specification;
import com.wsl.pojo.Tb_specification_option;
import com.wsl.tibet_sprcifcation.dao.Tb_specificationDao;
import com.wsl.service.Tb_specificationService;
import com.wsl.tibet_sprcifcation.dao.Tb_specifocation_optionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class Tb_specificationServiceImpl implements Tb_specificationService {
    @Autowired
    private Tb_specificationDao tb_specificationDao;
    @Autowired
    private Tb_specifocation_optionDao tb_specifocation_optionDao;
    @Override
    public void insert1(Tb_specification tb_specification) {
        tb_specificationDao.insert(tb_specification);
        int i =1;
        for (Tb_specification_option item:tb_specification.getTb_specification_option()){
            item.setOrders(i);
            i++;
            item.setSpec_id(tb_specification.getId());
            tb_specifocation_optionDao.insert(item);
        }

    }

    @Override
    public Tb_specification selectById(Long id) {
        Tb_specification tc = tb_specificationDao.selectById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("spec_id",id);
       List<Tb_specification_option> list = tb_specifocation_optionDao.selectList(queryWrapper);
                        tc.setTb_specification_option(list);
       return tc;
    }
    @Override
    public void deleteById(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
                     queryWrapper.eq("spec_id",id);
        tb_specifocation_optionDao.delete(queryWrapper);
        tb_specificationDao.deleteById(id);
    }

    @Override
    public void update(Tb_specification tb_specification) {
        tb_specificationDao.updateById(tb_specification);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("spec_id",tb_specification.getId());
        tb_specifocation_optionDao.delete(queryWrapper);
        int i =1;
        for (Tb_specification_option item:tb_specification.getTb_specification_option()){
            item.setOrders(i);
            i++;
            item.setSpec_id(tb_specification.getId());
            tb_specifocation_optionDao.insert(item);
        }
    }


}
